package com.uw.uwrpc.demo.consumer;

import com.uw.uwrpc.core.annotation.EnableUWReference;
import com.uw.uwrpc.core.annotation.UWReference;
import com.uw.uwrpc.core.api.RpcContext;
import com.uw.uwrpc.core.exception.RpcException;
import com.uw.uwrpc.demo.api.OrderService;
import com.uw.uwrpc.demo.api.UserService;
import com.uw.uwrpc.demo.api.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@Slf4j
@RestController
@EnableUWReference(scanBasePackages = "com.uw")
@SpringBootApplication
public class UwrpcDemoConsumerApplication {

    @UWReference
    private UserService userService;
    @UWReference
    private OrderService orderService;


    @RequestMapping("/")
    public User findById(@RequestParam("id") int id) {
        return userService.findById(id);
    }

    @RequestMapping("/timeout")
    public User timeout(@RequestParam("timeout") int timeout) {
        return userService.timeout(timeout);
    }


    public static void main(String[] args) {
        SpringApplication.run(UwrpcDemoConsumerApplication.class, args);
    }


    @Bean
    public ApplicationRunner consumerRunner() {
        return e -> {
            // List 测试用例
            log.info("===> userService.list(List<User>) result: " + userService.listByUser(Arrays.asList(
                    User.builder().id(11).name("uw").build(),
                    User.builder().id(22).name("uw").build(),
                    User.builder().id(33).name("uw").build()
            )));
            log.info("==================================================================");
            log.info("===> userService.list(List<Integer>) result: " + userService.list(Arrays.asList(1, 2, 3)));
            log.info("==================================================================");
            log.info("===> userService.list(1) result: " + userService.list(1));
            log.info("==================================================================");

            // 数组测试用例
            User[] userArray = {
                    User.builder().id(11).name("uw").build(),
                    User.builder().id(22).name("uw").build(),
                    User.builder().id(33).name("uw").build()
            };
            log.info("===> userService.findTypeByIds(User[]) result: " + Arrays.toString(userService.findTypeByIds(userArray)));
            log.info("==================================================================");
            log.info("===> userService.findTypeByIds(new int[]{1,2,3}) result: " + Arrays.toString(userService.findTypeByIds(new int[]{1, 2, 3})));
            log.info("==================================================================");
            log.info("===> userService.findTypeByIds(new long[]{100,200,300}) result: " + Arrays.toString(userService.findTypeByIds(new long[]{100, 200, 300})));
            log.info("==================================================================");

            // Void 测试用例
            userService.delete(User.builder().build());
            log.info("===> userService.delete(user) result: ");
            log.info("==================================================================");
            userService.deleteAll();
            log.info("===> userService.deleteAll() result: ");
            log.info("==================================================================");


            // int, long, Long 返回测试用例
            log.info("===> userService.count(1) result: " + userService.count(1));
            log.info("==================================================================");
            log.info("===> userService.count(1L) result: " + userService.count(1L));
            log.info("==================================================================");
            log.info("===> userService.count(Long.valueOf(1) result: " + userService.count(Long.valueOf(1)));
            log.info("==================================================================");


            // int, long, Long 参数测试用例
            log.info("===> userService.findById(0) result: " + userService.findById(0));
            log.info("==================================================================");
            log.info("===> userService.findById(1) result: " + userService.findById(1));
            log.info("==================================================================");
            log.info("===> userService.findById(1L) result: " + userService.findById(1L));
            log.info("==================================================================");
            log.info("===> userService.findById(user) result: " + userService.findById(User.builder().id(1).build()));
            log.info("==================================================================");

            // null, String, List<User> 测试用例
            log.info("===> userService.findById(0) result: " + userService.findById(0));
            log.info("==================================================================");
            log.info("===> userService.findNameById(1) result: " + userService.findNameById(1));
            log.info("==================================================================");
            log.info("===> userService.findTypeById(1) result: " + userService.findTypeById(1));
            log.info("==================================================================");

            // Object 方法测试用例
            log.info("===> userService.toString() result: " + userService.toString());
            log.info("==================================================================");
            log.info("===> userService.hashCode() result: " + userService.hashCode());
            log.info("==================================================================");


            log.info("Case 19. >>===[测试通过Context跨消费者和提供者进行传参]===");
            RpcContext.putContextParam("traceId", "v8");
            log.info("===> userService.echoParameter() result: {}", userService.echoParameter("traceId"));
            //log.info("==================================================================");


            log.info("Case 20. >>===[流控测试]===");
            log.info("===> userService.echoParameter() result: {}", userService.echoParameter("traceId"));
            for (int i = 0; i < 60; i++) {
                try {
                    Thread.sleep(1000);
                    RpcContext.putContextParam("traceId", "v8");
                    log.info("===> userService.echoParameter() {} result: {}",i, userService.echoParameter("traceId"));
                } catch (RpcException ex) {
                    // ignore
                    log.info("===> userService.echoParameter() {} result: {}", i, ex.getMessage());
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
            }

            // 抛异常测试用例
            //log.info("===> userService.findById(-1) result: " + userService.findById(-1));
            //log.info("==================================================================");
        };
    }

}
